package com.ruoyi.app.dyh.hxf.controller;

import com.ruoyi.app.dyh.hxf.domain.Active;
import com.ruoyi.app.dyh.hxf.service.IActiveService;

import com.ruoyi.app.dyh.hxf.service.IMyActiveService;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api("活动相关")
@RestController
@RequestMapping("/active")
public class ActiveController extends BaseController {

    @Autowired
    private IActiveService activeService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IMyActiveService myActiveService;


    @ApiOperation("删除活动")
    @Log(title = "删除活动", businessType = BusinessType.DELETE)
    @DeleteMapping("/{activeId}")
    public AjaxResult deleteActive(@PathVariable("activeId") String activeId) {
        return toAjax(activeService.deleteActive(activeId));
    }


    @ApiOperation("修改活动信息")
    @Log(title = "修改活动信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult updateActive(@Validated @RequestBody Active active) {
        active.setUpdateBy(getLoginUser().getUsername());
        return toAjax(activeService.updateActive(active));
    }


    @ApiOperation("根据活动id查询活动信息")
    @GetMapping("/{activeId}")
    public AjaxResult selectActiveById(@PathVariable("activeId") String activeId) {
        AjaxResult ajax = AjaxResult.success();

        // 获取登录用户的id
        String userId = String.valueOf(getLoginUser().getUserId());

        // 获取该活动的参加用户
        Active active = activeService.selectActiveById(activeId);
        ajax.put("active",active); // 放入活动信息
        String activeUserId = active.getActiveUserId();
        if (activeUserId.contains(userId)){
            ajax.put("isJoin",1);// 表示已加入
        }else {
            ajax.put("isJoin",0);// 表示未加入
        }
        String[] split = activeUserId.split(",");
        List<SysUser> userList = new ArrayList<>();
        for (int i = 0; i < split.length; i++) {
            // 将参加该活动的用户放到List中
            userList.add(userService.selectUserById(Long.valueOf(split[i])));
        }
        ajax.put("ActiveUserList",userList);

        Integer isManager = activeService.userIsActiveLeader(activeId,userId);
        if (isManager>0){
            ajax.put("isMaager",1); // 表示是管理员
        }else {
            ajax.put("isManager",0); //表示不是管理员
        }

        return ajax;
    }


    @ApiOperation("根据条件查询活动列表")
    @GetMapping("/list/{action}")
    public TableDataInfo selectActiveList(Active active, @PathVariable("action") int action) {
        startPage();
        List<Active> actives = activeService.selectActiveList(active,action);
        return getDataTable(actives);
    }

    @ApiOperation("加入该活动")
    @Log(title = "加入该活动", businessType = BusinessType.DELETE)
    @PutMapping("/{activeId}")
    public AjaxResult joinActive(@PathVariable("activeId") String activeId) {
        Active active = activeService.selectActiveById(activeId);
        String activeUserId = active.getActiveUserId();
        if (activeUserId!=null || !activeUserId.equals("")){
            activeUserId = activeUserId+","+getLoginUser().getUserId();
        }else if (activeUserId==null || activeUserId.equals("")){
            activeUserId = String.valueOf(getLoginUser().getUserId());
        }
        SysUser sysUser = userService.selectUserById(getLoginUser().getUserId());
        String activeIds = sysUser.getActiveIds();
        if (activeIds==null|| activeIds.equals("")){
            sysUser.setActiveIds(activeId);
        }else if (activeIds!=null || !activeIds.equals("")){
            activeIds = activeIds+","+activeId;
            sysUser.setActiveIds(activeIds);
        }
        myActiveService.updateUser(sysUser);
        return toAjax(activeService.updateActiveUserId(activeUserId,activeId));
    }


}
